package net.hnjdzy.tinydict;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import net.hnjdzy.tinydict.Fragment.Sentenceragment;
import net.hnjdzy.tinydict.Fragment.WordFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {



    private ViewPager viewPager;
    private RadioGroup radioGroup;

    private List<Fragment>fragments=new ArrayList<>();
    private  List<View>buttons;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager=(ViewPager)findViewById(R.id.jiemian);
        radioGroup=(RadioGroup)findViewById(R.id.anniu);
        Sentenceragment sentenceragment=new Sentenceragment();
        WordFragment wordFragment=new WordFragment();

        //将fragment放入集合
        fragments.add(wordFragment);
        fragments.add(sentenceragment);
        //适配器
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
       //得到单选组的所有按钮
       buttons=radioGroup.getTouchables();
       //给页面一个监听事件
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
              RadioButton radioButton=(RadioButton)buttons.get(position);
              radioButton.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //给点击主键设置一个监听事件
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.change_page_translation){
                    viewPager.setCurrentItem(0);
                }else if  (checkedId==R.id.change_page_Learning){
                    viewPager.setCurrentItem(1);
                }

            }
        });


    }

    private class MyAdapter extends  FragmentPagerAdapter {
        public MyAdapter(FragmentManager supportFragmentManager) {
            super( supportFragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
